﻿// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.

using System.Runtime.CompilerServices;

namespace SixLabors.ImageSharp.ColorSpaces.Companding;

/// <summary>
/// Implements gamma companding.
/// </summary>
/// <remarks>
/// <see href="http://www.brucelindbloom.com/index.html?Eqn_RGB_to_XYZ.html"/>
/// <see href="http://www.brucelindbloom.com/index.html?Eqn_XYZ_to_RGB.html"/>
/// </remarks>
public static class GammaCompanding
{
    /// <summary>
    /// Expands a companded channel to its linear equivalent with respect to the energy.
    /// </summary>
    /// <param name="channel">The channel value.</param>
    /// <param name="gamma">The gamma value.</param>
    /// <returns>The <see cref="float"/> representing the linear channel value.</returns>
    [MethodImpl(InliningOptions.ShortMethod)]
    public static float Expand(float channel, float gamma) => MathF.Pow(channel, gamma);

    /// <summary>
    /// Compresses an uncompanded channel (linear) to its nonlinear equivalent.
    /// </summary>
    /// <param name="channel">The channel value.</param>
    /// <param name="gamma">The gamma value.</param>
    /// <returns>The <see cref="float"/> representing the nonlinear channel value.</returns>
    [MethodImpl(InliningOptions.ShortMethod)]
    public static float Compress(float channel, float gamma) => MathF.Pow(channel, 1 / gamma);
}
